home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gnugo1_1.lha / gnugo / matchpat.c < prev    next >
C/C++ Source or Header  |  1989-03-07  |  5KB  |  198 lines

  1. /*
  2.                 GNU GO - the game of Go (Wei-Chi)
  3.                 Version 1.1   last revised 3-1-89
  4.            Copyright (C) Free Software Foundation, Inc.
  5.                       written by Man L. Li
  6.                       modified by Wayne Iba
  7.                     documented by Bob Webber
  8. */
  9. /*
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation - version 1.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License in file COPYING for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. Please report any bug/fix, modification, suggestion to
  24.  
  25. mail address:   Man L. Li
  26.                 Dept. of Computer Science
  27.                 University of Houston
  28.                 4800 Calhoun Road
  29.                 Houston, TX 77004
  30.  
  31. e-mail address: manli@cs.uh.edu         (Internet)
  32.                 coscgbn@uhvax1.bitnet   (BITNET)
  33.                 70070,404               (CompuServe)
  34. */
  35.  
  36. #include <stdio.h>
  37.  
  38. #define EMPTY 0
  39. #define MAXPC 16
  40. #define abs(x) ((x) < 0 ? -(x) : (x))
  41. #define line(x) (abs(x - 9))
  42.  
  43. extern unsigned char p[19][19];
  44. extern int mymove, umove;
  45. extern int lib;
  46.  
  47. matchpat(m, n, i, j, val)
  48. /* match pattern and get next move */
  49. int m, n, *i, *j, *val;
  50. {
  51.  struct patval {int x, y, att;}; /* pattern x, y coor and attribute */
  52. /* att = 0 - empty, 1 - your piece, 2 - my piece, 3 - my next move */
  53. /* 4 - empty on edge, 5 - your piece on edge, 6 - my piece on edge */
  54.  struct pattern {
  55.          struct patval patn[MAXPC];   /* pattern */
  56. /* number of pieces in pattern, no. of transformation, pattern value */
  57.          int patlen, trfno, patwt;
  58.            };
  59.  
  60. #include "patterns.c"
  61.  
  62. /* transformation matrice */
  63.  static int trf [8][2][2] = {
  64.    {{1, 0}, {0, 1}}, /* linear transfomation matrix */
  65.    {{1, 0}, {0, -1}},  /* invert */
  66.    {{0, 1}, {-1, 0}},  /* rotate 90 */
  67.    {{0, -1}, {-1, 0}},    /* rotate 90 and invert */
  68.    {{-1, 0}, {0, 1}},  /* flip left */
  69.    {{-1, 0}, {0, -1}},    /* flip left and invert */
  70.    {{0, 1}, {1, 0}},  /* rotate 90 and flip left */
  71.    {{0, -1}, {1, 0}}  /* rotate 90, flip left and invert */
  72.  };
  73.  int k, my, nx, l, r, cont;
  74.  int ti, tj, tval;
  75.  
  76.  *i = -1;   *j = -1;   *val = -1;
  77.  for (r = 0; r < PATNO; r++)
  78. /* try each pattern */
  79.     for (l = 0; l < pat[r].trfno; l++)
  80. /* try each orientation transformation */
  81.       {
  82.        k = 0;  cont = 1;
  83.        while ((k != pat[r].patlen) && cont)
  84. /* match each point */
  85.      {
  86. /* transform pattern real coordinate */
  87.       nx = n + trf[l][0][0] * pat[r].patn[k].x
  88.          + trf[l][0][1] * pat[r].patn[k].y;
  89.       my = m + trf[l][1][0] * pat[r].patn[k].x
  90.          + trf[l][1][1] * pat[r].patn[k].y;
  91.  
  92. /* outside the board */
  93.       if ((my < 0) || ( my > 18) || (nx < 0) || (nx > 18))
  94.         {
  95.          cont = 0;
  96.          break;
  97.        }
  98.       switch (pat[r].patn[k].att) {
  99.       case 0 : if (p[my][nx] == EMPTY)    /* open */
  100.               break;
  101.            else
  102.              {
  103.               cont = 0;
  104.               break;
  105.             }
  106.       case 1 : if (p[my][nx] == umove)  /* your piece */
  107.               break;
  108.            else
  109.              {
  110.               cont = 0;
  111.               break;
  112.             }
  113.       case 2 : if (p[my][nx] == mymove)  /* my piece */
  114.               break;
  115.            else
  116.              {
  117.               cont = 0;
  118.               break;
  119.             }
  120.       case 3 : if (p[my][nx] == EMPTY)    /* open for new move */
  121.             {
  122.              lib = 0;
  123.              countlib(my, nx, mymove);    /* check liberty */
  124.              if (lib > 1)  /* move o.k. */
  125.                {
  126.             ti = my;
  127.             tj = nx;
  128.             break;
  129.                }
  130.              else
  131.                {
  132.             cont = 0;
  133.             break;
  134.               }
  135.              }
  136.            else
  137.              {
  138.               cont = 0;
  139.               break;
  140.             }
  141.       case 4 : if ((p[my][nx] == EMPTY)  /* open on edge */
  142.                && ((my == 0) || (my == 18) || (nx == 0) || (nx == 18)))
  143.               break;
  144.            else
  145.              {
  146.               cont = 0;
  147.               break;
  148.             }
  149.       case 5 : if ((p[my][nx] == umove)  /* your piece on edge */
  150.                && ((my == 0) || (my == 18) || (nx == 0) || (nx == 18)))
  151.               break;
  152.            else
  153.              {
  154.               cont = 0;
  155.               break;
  156.             }
  157.       case 6 : if ((p[my][nx] == mymove)  /* my piece on edge */
  158.                && ((my == 0) || (my == 18) || (nx == 0) || (nx == 18)))
  159.               break;
  160.            else
  161.              {
  162.               cont = 0;
  163.               break;
  164.             }
  165.          }
  166.       ++k;
  167.      }
  168.      if (cont)   /* match pattern */
  169.        {
  170.         tval = pat[r].patwt;
  171.         if ((r >= 8) && (r <= 13))    /* patterns for expand region */
  172.           {
  173.            if (line(ti) > 7)  /* penalty on line 1, 2 */
  174.           tval--;
  175.            else
  176.           if ((line(ti) == 6) || (line(ti) == 7))
  177.              tval++;    /* reward on line 3, 4 */
  178.  
  179.            if (line(tj) > 7)  /* penalty on line 1, 2 */
  180.           tval--;
  181.            else
  182.           if ((line(tj) == 6) || (line(tj) == 7))
  183.              tval++;    /* reward on line 3, 4 */
  184.          }
  185.         if (tval > *val)
  186.           {
  187.            *val = tval;
  188.            *i = ti;
  189.            *j = tj;
  190.          }
  191.       }
  192.       }
  193.  if (*val > 0)    /* pattern matched */
  194.     return 1;
  195.  else  /* match failed */
  196.     return 0;
  197. }  /* end matchpat */
  198.